import { ref, computed } from 'vue';

// 引入 市场数据
import { stock_list } from '@/service/market';
import { trading } from '@/server/trading';
import { stock_close_order, assetService, closeStockCloseModel } from '@/service/asset';

// 当前选择的股票
export const active_stock = ref(stock_list.value[0]);
// 搜索的股票
export const search_stock = ref('');
// 计算属性自动搜索
export const show_stock = computed(() => {
    if (search_stock.value) {
        return stock_list.value.filter(item => item.symbol.toLowerCase().includes(search_stock.value.toLowerCase()));
    } else {
        return stock_list.value;
    }
});
// 右侧的交易类型 买入 还是 卖出
export const tabbar = [
    { id: 1, text: 'Buy' },
    { id: 2, text: 'Sell' },
];
// 选择的交易类型
export const active_tabbar = ref(tabbar[0]);

// 交易规则
export const trading_type_list = [
    { id: 1, text: 'Market', value: 0 },
    { id: 2, text: 'Limit', value: 1 },
];
// 选择的交易规则
export const active_trading_type = ref(trading_type_list[0].value);

// 交易数量
export const quantity = ref(1);
// 价格限制
export const limit_price = ref(0);

// 总价格
export const total_price = computed(() => {
    if (!active_stock.value) return '0.00';
    let price = active_stock.value?.price?.slice(1);
    return `$${ (price * quantity.value).toFixed(2) }`;
});

export const tradingService = {
    async stockOpen() {
        let marketId = active_stock.value.id;
        let count = quantity.value;
        let openType = active_trading_type.value;
        let price = limit_price.value;
        let res = await trading.stockOpen(marketId, count, openType, openType == 1 ? price : null);
        if (res.success) {
            if (res.data.code == 200) {
                toast.success('Trading success!');
                assetService.getStockOrderList();
            } else if (res.data.code == 401) {
                toast.warning('Trading Failed! Please try again after login!', 2000);
            } else {
                toast.warning('Trading failed!');
            }
        } else {
            if (res.err.message == 'token is null') {
                toast.warning('Trading Failed! Please try again after login!', 2000);
            } else {
                toast.warning('Trading failed!');
            }
        }
    },
    async stockClose() {
        let marketId = stock_close_order.value.id;
        let count = stockCloseQuantity.value;
        let openType = active_trading_type.value;
        let price = limit_price.value;
        let res = await trading.stockClose(marketId, count, openType, openType == 1 ? price : null);
        if (res.success) {
            if (res.data.code == 200) {
                toast.success('Trading success!');
                assetService.getStockOrderList();
            } else if (res.data.code == 401) {
                toast.warning('Trading Failed! Please try again after login!', 2000);
            } else {
                toast.warning('Trading failed!');
            }
        } else {
            if (res.err.message == 'token is null') {
                toast.warning('Trading Failed! Please try again after login!', 2000);
            } else {
                toast.warning('Trading failed!');
            }
        }
    }
};

// 平仓数量
export const stockCloseQuantity = ref(0);
// 平仓市场价
export const stockCloseMarketPrice = computed(() => {
    for (let i = 0; i < stock_list.value.length; i++) {
        console.log('检查', stock_close_order, stock_close_order.marketSymbol, stock_close_order.value.marketSymbol, stock_list.value[i]);
        if (stock_close_order.value.marketSymbol == stock_list.value[i].symbol) {
            return stock_list.value[i].price;
        }
    }
});
// 平仓总价
export const stockCloseTotalPrice = computed(() => {
    if (!stockCloseMarketPrice.value) return 0;
    let price = parseFloat(stockCloseMarketPrice.value.slice(1));
    console.log('price', stockCloseMarketPrice.value, price);
    return `$${ (price * stockCloseQuantity.value).toFixed(2) }`;
});